using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.WizardFramework;
using Microsoft.Practices.Common;
using Microsoft.Practices.Common.Services;
using Microsoft.Practices.ComponentModel;
using System.ComponentModel.Design;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library;

namespace Microsoft.Practices.RepositoryFactory.CustomPages
{
    /// <summary>
    /// Add some validation to the standard SpecifyProjectResponsibility page.
    /// </summary>
    [ServiceDependency(typeof(IDictionaryService))]
    public class SpecifyProjectResponsibilityCP : CustomWizardPage
    {
        private System.Windows.Forms.CheckBox HostProject;
        private System.Windows.Forms.CheckBox DataAccessProject;
        private System.Windows.Forms.ToolTip toolTip;
        private System.ComponentModel.IContainer components;
        private System.Windows.Forms.CheckBox BusinessEntitiesProject;
        private Project project;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:SpecifyProjectResponsibilityCP"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public SpecifyProjectResponsibilityCP(WizardForm parent)
            : base(parent)
        {
            InitializeComponent();
        }

        [RecipeArgument]
        public Project Project
        {
            set
            {
                // Check which checkbox/es we need to disable
                bool isWeb = DteHelper.IsWebProject(value);
                BusinessEntitiesProject.Enabled = !isWeb;
                DataAccessProject.Enabled = !isWeb;
                this.project = value;
            }
            get { return project; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is business entities project.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is business entities project; otherwise, <c>false</c>.
        /// </value>
        [RecipeArgument]
        public bool IsBusinessEntityProject
        {
            set
            {
                if (this.BusinessEntitiesProject.Checked == value)
                {
                    // Force argument update
                    OnCheckedChanged(this.BusinessEntitiesProject, new EventArgs());
                }
                this.BusinessEntitiesProject.Checked = value;
            }
            get { return BusinessEntitiesProject.Checked; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is data access project.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is data access project; otherwise, <c>false</c>.
        /// </value>
        [RecipeArgument]
        public bool IsDataAccessProject
        {
            set
            {
                if (this.DataAccessProject.Checked == value)
                {
                    // Force argument update
                    OnCheckedChanged(this.DataAccessProject, new EventArgs());
                }
                this.DataAccessProject.Checked = value; 
            }
            get { return DataAccessProject.Checked; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is host project.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is host project; otherwise, <c>false</c>.
        /// </value>
        [RecipeArgument]
        public bool IsHostProject
        {
            set 
            {
                if (this.HostProject.Checked == value)
                {
                    // Force argument update
                    OnCheckedChanged(this.HostProject, new EventArgs());
                }
                this.HostProject.Checked = value;                 
            }
            get { return HostProject.Checked; }
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SpecifyProjectResponsibilityCP));
            this.BusinessEntitiesProject = new System.Windows.Forms.CheckBox();
            this.DataAccessProject = new System.Windows.Forms.CheckBox();
            this.HostProject = new System.Windows.Forms.CheckBox();
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            this.infoPanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // infoPanel
            // 
            this.infoPanel.Controls.Add(this.HostProject);
            this.infoPanel.Controls.Add(this.DataAccessProject);
            this.infoPanel.Controls.Add(this.BusinessEntitiesProject);
            resources.ApplyResources(this.infoPanel, "infoPanel");
            this.infoPanel.Controls.SetChildIndex(this.BusinessEntitiesProject, 0);
            this.infoPanel.Controls.SetChildIndex(this.DataAccessProject, 0);
            this.infoPanel.Controls.SetChildIndex(this.HostProject, 0);
            // 
            // BusinessEntitiesProject
            // 
            resources.ApplyResources(this.BusinessEntitiesProject, "BusinessEntitiesProject");
            this.BusinessEntitiesProject.Name = "BusinessEntitiesProject";
            this.BusinessEntitiesProject.Tag = "IsBusinessEntityProject";
            this.toolTip.SetToolTip(this.BusinessEntitiesProject, resources.GetString("BusinessEntitiesProject.ToolTip"));
            this.BusinessEntitiesProject.UseVisualStyleBackColor = true;
            this.BusinessEntitiesProject.CheckedChanged += new System.EventHandler(this.OnCheckedChanged);
            // 
            // DataAccessProject
            // 
            resources.ApplyResources(this.DataAccessProject, "DataAccessProject");
            this.DataAccessProject.Name = "DataAccessProject";
            this.DataAccessProject.Tag = "IsDataAccessProject";
            this.toolTip.SetToolTip(this.DataAccessProject, resources.GetString("DataAccessProject.ToolTip"));
            this.DataAccessProject.UseVisualStyleBackColor = true;
            this.DataAccessProject.CheckedChanged += new System.EventHandler(this.OnCheckedChanged);
            // 
            // HostProject
            // 
            resources.ApplyResources(this.HostProject, "HostProject");
            this.HostProject.Name = "HostProject";
            this.HostProject.Tag = "IsHostProject";
            this.toolTip.SetToolTip(this.HostProject, resources.GetString("HostProject.ToolTip"));
            this.HostProject.UseVisualStyleBackColor = true;
            this.HostProject.CheckedChanged += new System.EventHandler(this.OnCheckedChanged);
            // 
            // SpecifyProjectResponsibilityCP
            // 
            resources.ApplyResources(this, "$this");
            this.Name = "SpecifyProjectResponsibilityCP";
            this.infoPanel.ResumeLayout(false);
            this.infoPanel.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void OnCheckedChanged(object sender, EventArgs e)
        {
            System.Windows.Forms.CheckBox checkbox = (System.Windows.Forms.CheckBox)sender;
            IDictionaryService dictionary = (IDictionaryService)this.GetService(typeof(IDictionaryService));
            dictionary.SetValue(checkbox.Tag, checkbox.Checked);
        }
    }
}
